﻿#region Header

// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------

#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using ScrumTable.Common.Properties;

#endregion

namespace ScrumTable.Common.Security
{
    /// <summary>
    /// Represents the information manager needed in order to establish
    /// a connection to an external, windows based system.
    /// </summary>
    public class ConnectionInformationManager : IEnumerable<ConnectionInformation>
    {
        #region Declarations

        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IDictionary<string, ConnectionInformation> _connections = new Dictionary<string, ConnectionInformation>();
        private ConnectionInformation _selected;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the ConnectionInformation with the corresponding unqiue id.
        /// </summary>
        /// <param name="backEndId">Specifies the unqiue id of the connection information element to retrieve.</param>
        /// <returns>Returns the retrieved connection information element.</returns>
        public ConnectionInformation this[string backEndId]
        {
            get
            {
                PreCondition.AssertNotNullOrEmpty(backEndId, "backEndId");
                ConnectionInformation information = RetrieveFromUniqueId(backEndId);

                if (information == null)
                    throw new InvalidOperationException(string.Format(Resources.ExcConInfoMgrUniqueIdUnavailable, backEndId));

                return information;
            }
        }

        /// <summary>
        /// Gets the currently selected connection information.
        /// </summary>
        public ConnectionInformation Selected
        {
            get { lock (this) { return _selected; } }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new ConnectionInformationManager instance.
        /// </summary>
        public ConnectionInformationManager(params ConnectionInformation[] informations)
        {
            if (informations != null)
            {
                Array.ForEach(
                    informations,
                    ci =>
                        {
                            if (ci != null)
                                _connections[ci.InternalId] = ci;
                        }
                    );

                if (informations.Length > 0)
                    Select(informations[0]);
            }
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Selectes the connection information with the given id (mark as default).
        /// </summary>
        /// <param name="backEndId">Specifies the unqiue id of the selected connection information.</param>
        public void Select(string backEndId)
        {
            Select(this[backEndId]);
        }

        /// <summary>
        /// Selectes the connection information with the given id.
        /// </summary>
        /// <param name="toSelect">Specifies the connection information to select (mark as default).</param>
        public void Select(ConnectionInformation toSelect)
        {
            lock (this)
            {
                _selected = _connections[toSelect.InternalId];
            }
        }

        /// <summary>
        /// Checks whether the current manager contains a connection information
        /// instance with the given unique id.
        /// </summary>
        /// <param name="backEndId">Specifies the unique id of the connection information to retrieve.</param>
        /// <returns>Returns true if a connection information instance could be found.</returns>
        public bool Contains(string backEndId)
        {
            return (RetrieveFromUniqueId(backEndId) != null);
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<ConnectionInformation> GetEnumerator()
        {
            return _connections.Values.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        private ConnectionInformation RetrieveFromUniqueId(string backEndId)
        {
            return (from connectionInfo in _connections
                    where connectionInfo.Value.BackEndId == backEndId
                    select connectionInfo.Value).FirstOrDefault();
        }

        #endregion
    }
}